package com.ruoyi.plus.pub.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.plus.constants.PlusDictConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.plus.pub.domain.PubNoticePlus;
import com.ruoyi.plus.pub.domain.PubNoticeReceiver;
import com.ruoyi.plus.pub.mapper.PubNoticePlusMapper;
import com.ruoyi.plus.pub.mapper.PubNoticeReceiverMapper;
import com.ruoyi.plus.pub.service.IPubNoticeReceiverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统公告通知接收人Service业务层处理
 *
 */
@Service
public class PubNoticeReceiverServiceImpl extends ServiceImpl<PubNoticeReceiverMapper, PubNoticeReceiver> implements IPubNoticeReceiverService {

    @Autowired
    private PubNoticeReceiverMapper pubNoticeReceiverMapper;
    @Autowired
    private PubNoticePlusMapper pubNoticePlusMapper;

    /**
     * 查询系统公告通知接收人列表
     *
     * @param pubNoticeReceiver 系统公告通知接收人
     * @return 系统公告通知接收人
     */
    @Override
    public List<PubNoticeReceiver> selectPubNoticeReceiverList(PubNoticeReceiver pubNoticeReceiver) {
        return pubNoticeReceiverMapper.selectPubNoticeReceiverList(pubNoticeReceiver);
    }

    @Override
    public void saveReceivers(PubNoticePlus pubNoticePlus, List<Long> userIds) {
        List<PubNoticeReceiver> list = new ArrayList<>();
        for (Long userId : userIds) {
            PubNoticeReceiver sr = new PubNoticeReceiver();
            sr.setNoticeId(pubNoticePlus.getId());
            sr.setUserId(userId);
            sr.setNoticeStatus(PlusDictConstants.NOTICE_PLUS_STATUS_RELEASE);
            sr.setNoticeStartTime(pubNoticePlus.getNoticeStartTime());
            sr.setNoticeEndTime(pubNoticePlus.getNoticeEndTime());
            sr.setSystemType(pubNoticePlus.getSystemType());

            list.add(sr);
        }
        if (list.size() > 0) {
            this.saveBatch(list);
        }
    }

    @Override
    public List<Long> noticeReceiverIds(Long noticeId) {
        return pubNoticeReceiverMapper.noticeReceiverIds(noticeId);
    }

    @Override
    public void deleteReceiversByNoticeId(Long noticeId) {
        pubNoticeReceiverMapper.deleteReceiversByNoticeId(noticeId);
    }

    @Override
    public int unReadCount(Long userId,String systemType) {
        return pubNoticeReceiverMapper.unReadCount(userId,systemType);
    }

    @Override
    public int unReadCountByRepair(Long userId) {
        return pubNoticeReceiverMapper.unReadCountByRepair(userId);
    }

    @Override
    public int updateNoticeStatus(PubNoticeReceiver pubNoticeReceiver) {
        return pubNoticeReceiverMapper.updateNoticeStatus(pubNoticeReceiver);
    }

    @Override
    public int updateReadStatus(PubNoticeReceiver pubNoticeReceiver) {
        return pubNoticeReceiverMapper.updateReadStatus(pubNoticeReceiver);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAndSaveReceivers(List<Long> userIds, PubNoticePlus pubNoticePlus) {
        List<Long> oldReceiverIds = this.noticeReceiverIds(pubNoticePlus.getId());
        //求差集(userIdsList - oldReceiverIds) 用于插入
        List<Long> addUserIds = userIds.stream().filter(item -> !oldReceiverIds.contains(item)).collect(Collectors.toList());
        if (addUserIds.size() > 0) {
            saveReceivers(pubNoticePlus, addUserIds);
        }

        //求差集(oldReceiverIds - userIdsList) 用于删除
        List<Long> delUserIds = oldReceiverIds.stream().filter(item -> !userIds.contains(item)).collect(Collectors.toList());
        if (delUserIds.size() > 0) {
            pubNoticeReceiverMapper.deleteReceivers(pubNoticePlus.getId(), delUserIds);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveNoticeAndReceivers(Long[] userIds, String msg) {
        if (userIds.length > 0) {
            PubNoticePlus pubNoticePlus = new PubNoticePlus();
            pubNoticePlus.setTitle(msg);
            pubNoticePlus.setSummary(msg);
            pubNoticePlus.setContent(msg);
            pubNoticePlus.setStatus(PlusDictConstants.NOTICE_PLUS_STATUS_RELEASE);
            pubNoticePlus.setType(PlusDictConstants.NOTICE_PLUS_TYPE_PRIVATE);
            pubNoticePlus.setNoticeStartTime(new Date());
            pubNoticePlus.setNoticeEndTime(DateUtils.dateTime("yyyy", "2099"));
            pubNoticePlusMapper.insert(pubNoticePlus);
            saveReceivers(pubNoticePlus, Arrays.asList(userIds));
        }
    }
}
